<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
 	https://ai.baidu.com/ai-doc/NLP/vk6z52h5n
*/
namespace jy_aiaddress;
defined('BY_JYA') or exit('error');
class api extends \plugin{
	public function __construct(){
		$this->basic=plugin('jy_aiaddress')->plugin_setting('basic');
		if(!$this->basic['isopen']){
			return false;
		}
		if(!$this->basic['api'] || !$this->basic['secret']){
			exi('未填写应用API Key、Secret Key','error');
		}
		if($this->basic['expires_in']<TIMESTAMP || !$this->basic['access_token']){
			$this->getAccessToken();
		}
	}
	public function getAccessToken($continue=false){//获取access_token
		global $_AR;
		if($this->basic['expires_in']>TIMESTAMP && !$continue && $this->basic['access_token']){
			return $this->basic['access_token'];
		}
		if(!$this->basic['secret']){
			return false;
		}else{
			$url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={$this->basic['api']}&client_secret={$this->basic['secret']}";
		}
		$content = cfc('http')->ihttp_request($url);
		if(is_error($content)){
			exi('获取百度AI授权失败, 请稍后重试！错误详情：'.$content['message'],'error');
		}
		$token = @json_decode($content['content'], true);
		if(empty($token) || empty($token['access_token'])) {
			exi('获取百度AI授权失败, 请稍后重试！错误代码-' . $token['error'] . '，错误信息-' . $token['error_description'],'error');
		}
		$record = array();
		$this->basic['access_token'] = $token['access_token'];
		$this->basic['expires_in'] = TIMESTAMP + $token['expires_in'] - 200;
		plugin('jy_aiaddress')->plugin_setting('basic',$this->basic);
		return $token['access_token'];
	}
	//发起http请求
	public function request($url,$post=array()){
		if(!$this->basic['isopen']){
			return false;
		}
		if($this->return_error_one){
			$return_error=true;
			$this->return_error_one=false;
		}
		if($this->return_error){
			$return_error=true;
		}
		$rurl=str_replace(array('{access_token}','ACCESS_TOKEN'),$this->basic['access_token'],$url);
		$headers = array('Content-Type' => 'application/json');
		$post=ijson_encode($post);
		$result=cfc('http')->ihttp_json($rurl,$post);
		if(is_error($result)){			
			$message="访问失败, 错误: {$result['message']}";
			if($return_error){
				return error($message);
			}else{
				exi($message,'error');
			}
		}
		$data=preg_replace('/[\x00-\x1F]/','',$result['content']);
		$data=@json_decode($data,true);
		if(($data['errcode']=='40001' || $data['errcode']=='42001') && $continue){
			$this->getAccessToken(true);
			$data=$this->request($url,$post,false,$direct,$return_error);
			return $data;
		}
		
		if($data['errcode']){
			$message=$this->error_code($data['errcode']).' '.$data['errmsg'].' '.$data['errcode'];
			if($return_error || $_SESSION['this']=='custom'){
				//return error($message);
			}else{
				//exi($message,'error');
			}
		}
		if($direct){
			return $result;
		}
		if(!is_array($data) || !$data){
			$message='访问百度接口错误，以下为原始数据：'.$result['content'];
			if($return_error){
				return error($message);
			}else{
				exi($message,'error');
			}
		}
		return $data;
	}
	//识别地址
	public function getaddress($text='') {
		if(!$text){
			return false;
		}
		if(!$this->basic['isopen']){
			return false;
		}
		$url = "https://aip.baidubce.com/rpc/2.0/nlp/v1/address?access_token={access_token}";
		$res=$this->request($url,array('text'=>$text));
		if(!$res['city']){
			$res['errcode']=$res['errcode']?:$res['error_code'];
			$res['errmsg']=$res['errmsg']?:$res['error_msg'];
			exi('识别地址失败：'.$res['errcode'].','.$res['errmsg'],'error');
		}
		return $res;
	}
}